package com.example.restatr.Atcivity.YouthInn;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.cardview.widget.CardView;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.example.restatr.Atcivity.Main.MainActivity;
import com.example.restatr.Atcivity.YouthInn.Bean.TalentPolicyAreaBean;
import com.example.restatr.Atcivity.YouthInn.Bean.YouthInnBean;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class YouthInnAt extends AppCompatActivity {
    private Toolbar toolbar;
    private TextView barTitle;
    private EditText search;
    private TabLayout tab;
    private ListView lv;
    private List<TalentPolicyAreaBean.DataBean> dataBeans;
    private boolean isPolicy = true;
    private List<YouthInnBean.RowsBean> rowsBeans;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_youth_inn_at);

        toolbar = findViewById(R.id.toolbar);
        barTitle = findViewById(R.id.bar_Title);
        search = findViewById(R.id.search);
        tab = findViewById(R.id.tab);
        lv = findViewById(R.id.lv);

        Tools.SetBarTitle(toolbar, barTitle, "青年驿站", search, this, false);
        GetTalentPolicyArea();
        tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                String s = tab.getText().toString();
                if (s.equals("人才政策")) {
                    GetTalentPolicyArea();
                    isPolicy = true;
                } else if (s.equals("驿站")) {
                    isPolicy = false;
                    GetYouthInn();
                }
            }


            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }

    private void GetYouthInn() {
        new Thread(() -> {
            String token = Tools.Get("/prod-api/api/youth-inn/youth-inn/list", MainActivity.sp.getString("token", ""));
            YouthInnBean youthInnBean = new Gson().fromJson(token, YouthInnBean.class);
            rowsBeans = new ArrayList<>();
            rowsBeans.addAll(youthInnBean.getRows());
            runOnUiThread(() -> {
                if (rowsBeans.size() > 0) {
                    lv.setAdapter(new adapter());
                }
            });
        }).start();
    }

    private void GetTalentPolicyArea() {
        new Thread(() -> {
            String token = Tools.Get("/prod-api/api/youth-inn/talent-policy-area/list", MainActivity.sp.getString("token", ""));
            TalentPolicyAreaBean talentPolicyAreaBean = new Gson().fromJson(token, TalentPolicyAreaBean.class);
            dataBeans = new ArrayList<>();
            dataBeans.addAll(talentPolicyAreaBean.getData());
            runOnUiThread(() -> {
                if (dataBeans.size() > 0) {
                    lv.setAdapter(new adapter());
                }
            });
        }).start();
    }

    class adapter extends BaseAdapter {

        @Override
        public int getCount() {
            if (isPolicy) {
                return dataBeans.size();
            } else {
                return rowsBeans.size();
            }
        }

        @Override
        public Object getItem(int position) {
            if (isPolicy) {
                return dataBeans.get(position);
            } else {
                return rowsBeans.size();
            }
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = getLayoutInflater().inflate(R.layout.zfz_list_item, null);
            ImageView tupian = convertView.findViewById(R.id.tupian);
            TextView name = convertView.findViewById(R.id.name);
            TextView diz = convertView.findViewById(R.id.diz);
            TextView phone = convertView.findViewById(R.id.phone);
            TextView mj = convertView.findViewById(R.id.mj);
            TextView hux = convertView.findViewById(R.id.hux);
            TextView jg = convertView.findViewById(R.id.jg);
            CardView cardBtn = convertView.findViewById(R.id.cardBtn);

            if (isPolicy) {
                Glide.with(YouthInnAt.this)
                        .load(Tools.ip + dataBeans.get(position).getImgUrl())
                        .transform(new RoundedCorners(15))
                        .into(tupian);
                name.setText(dataBeans.get(position).getName());
            } else {
                Glide.with(YouthInnAt.this)
                        .load(Tools.ip + rowsBeans.get(position).getCoverImgUrl())
                        .transform(new RoundedCorners(15))
                        .into(tupian);
                name.setText(rowsBeans.get(position).getName());
                diz.setText(rowsBeans.get(position).getAddress());
                phone.setText(rowsBeans.get(position).getPhone());
                mj.setText("床位 " + rowsBeans.get(position).getBedsCountBoy() + "/男 ");
                hux.setText(rowsBeans.get(position).getBedsCountGirl() + "/女");
                jg.setText(rowsBeans.get(position).getWorkTime());
            }

            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    if (isPolicy) {
                        intent.setClass(YouthInnAt.this, AreaInfoAt.class);
                        GetAreaInfo(dataBeans.get(position).getId(), intent);
                    } else {
                        intent.setClass(YouthInnAt.this, YouthInnInfoAt.class);
                        GetInnInfo(rowsBeans.get(position).getId(), intent);
                    }
                }
            });

            return convertView;
        }
    }

    private void GetInnInfo(int id, Intent intent) {
        new Thread(() -> {
            String token = Tools.Get("/prod-api/api/youth-inn/youth-inn/" + id, MainActivity.sp.getString("token", ""));
            intent.putExtra("data", token);
            startActivity(intent);
        }).start();
    }

    private void GetAreaInfo(int id, Intent intent) {
        new Thread(() -> {
            String token = Tools.Get("/prod-api/api/youth-inn/talent-policy-area/" + id, MainActivity.sp.getString("token", ""));
            intent.putExtra("data", token);
            startActivity(intent);
        }).start();
    }
}